package cn.wangzg.course0901;

import androidx.appcompat.app.AppCompatActivity;

import android.Manifest;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.ContentObserver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.provider.ContactsContract;
import android.view.View;

import java.util.List;

public class MainActivity extends BaseActivity {
    private String[] permiss={Manifest.permission.READ_SMS};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        getContentResolver().registerContentObserver(Uri.parse("content://sms/"),true,
                new MyObserver(new Handler()));
    }

    public void btnReadSms(View view){
        requestRunPermisssion(permiss, new PermissionListener() {
            @Override
            public void onGranted() {
                getSms();
            }

            @Override
            public void onDenied(List<String> deniedPermission) {

            }
        });

    }

    private void getSms() {
        Uri uri = Uri.parse("content://sms/");
        ContentResolver resolver = getContentResolver();
        //获取的是哪些列的信息
        Cursor cursor = resolver.query(uri, new String[]{"address","date","type","body"}, null, null, null);
        while(cursor.moveToNext())
        {
            String address = cursor.getString(0);
            String date = cursor.getString(1);
            String type = cursor.getString(2);
            String body = cursor.getString(3);
            System.out.println("地址:" + address);
            System.out.println("时间:" + date);
            System.out.println("类型:" + type);
            System.out.println("内容:" + body);
            System.out.println("======================");
        }
        cursor.close();
    }
    public void btnReadLxr(View view){
        requestRunPermisssion(new String[]{Manifest.permission.READ_CONTACTS}, new PermissionListener() {
            @Override
            public void onGranted() {
                getContacts();
            }

            @Override
            public void onDenied(List<String> deniedPermission) {

            }
        });

    }
    private void getContacts(){
        //①查询raw_contacts表获得联系人的id
        ContentResolver resolver = getContentResolver();
        Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
        //查询联系人数据
        Cursor cursor = resolver.query(uri, null, null, null, null);
        while(cursor.moveToNext())
        {
            //获取联系人姓名,手机号码
            String cName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
            String cNum = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
            System.out.println("姓名:" + cName);
            System.out.println("号码:" + cNum);
            System.out.println("======================");
        }
        cursor.close();
    }

    private final class MyObserver extends ContentObserver{

        /**
         * Creates a content observer.
         *
         * @param handler The handler to run {@link #onChange} on, or null if none.
         */
        public MyObserver(Handler handler) {
            super(handler);
        }

        @Override
        public void onChange(boolean selfChange, Uri uri) {
            //super.onChange(selfChange, uri);
            System.out.println(uri.toString());
            if(uri.toString().startsWith("content://sms/raw")){
                return;
            }
            Cursor cursor = getContentResolver().query(uri,
                    new String[]{"address","date","type","body"},
                    null, null, null);
            while (cursor.moveToNext()){
                String address = cursor.getString(0);
                String date = cursor.getString(1);
                String type = cursor.getString(2);
                String body = cursor.getString(3);
                System.out.println("地址:" + address);
                System.out.println("时间:" + date);
                System.out.println("类型:" + type);
                System.out.println("内容:" + body);
                System.out.println("======================");
            }
        }
    }
}
